<!--
 * $Description: 用于demo展示，从element 源码中拿到
 * $Author BPF
 * $create 2021/12/5 14:26
 -->
<template>
  <div
    class="demo-block"
    :class="[blockClass, { hover: hovering }]"
    $mouseenter="hovering = true"
    $mouseleave="hovering = false"
  >
    <div class="source">
      <slot name="source"></slot>
    </div>
    <div ref="meta" class="meta">
      <div v-if="$slots.default" class="description">
        <slot></slot>
      </div>
      <div class="highlight">
        <slot name="highlight"></slot>
      </div>
    </div>
    <div ref="control" class="demo-block-control" :class="{ 'is-fixed': false }" $click="isExpanded = !isExpanded">
      <transition name="arrow-slide">
        <i :class="['iconfont-bi', iconClass, { hovering: hovering }]" style="display: inline-block"></i>
      </transition>
      <transition name="text-slide">
        <span v-show="hovering">{{ controlText }}</span>
      </transition>
      <transition name="text-slide">
        <span v-show="hovering" class="control-button" $click.stop="onCopyCode"> {{ langConfig['copy-text'] }}</span>
      </transition>
    </div>
  </div>
</template>

<script>
import { stripScript, stripStyle, stripTemplate } from './util.js'
import { debounce } from '@bianpengfei/utils'
export default {
  name: 'GDemoBlock',
  data() {
    return {
      codepen: {
        script: '',
        html: '',
        style: ''
      },
      hovering: false,
      isExpanded: false,
      fixedControl: false,
      scrollParent: null
    }
  },
  computed: {
    lang () {
      return 'zh-CN'
    },
    langConfig () {
      return {
        'hide-text': '隐藏代码',
        'show-text': '显示代码',
        'copy-text': '复制代码',
        'copy-success': '复制成功'
      }
    },
    blockClass () {
      return `demo-${this.lang} demo-${this.$router.currentRoute.path.split('/').pop()}`
    },
    iconClass () {
      return this.isExpanded ? 'icon-bi-xiangshang1' : 'icon-bi-xiangxia1'
    },
    controlText () {
      return this.isExpanded ? this.langConfig['hide-text'] : this.langConfig['show-text']
    },
    codeArea () {
      return this.$el.getElementsByClassName('meta')[0]
    },
    codeAreaHeight () {
      if (this.$el.getElementsByClassName('description').length > 0) {
        return (
          this.$el.getElementsByClassName('description')[0].clientHeight +
          this.$el.getElementsByClassName('highlight')[0].clientHeight +
          20
        )
      }
      return this.$el.getElementsByClassName('highlight')[0].clientHeight
    }
  },
  watch: {
    isExpanded (val) {
      this.codeArea.style.height = val ? `${this.codeAreaHeight + 1}px` : '0'
      if (!val) {
        this.fixedControl = false
        this.$refs.control.style.left = '0'
        this.removeScrollHandler()
        return
      }
      setTimeout(() => {
        this.scrollParent = document.querySelector('.page-component__scroll > .el-scrollbar__wrap')
        this.scrollParent && this.scrollParent.addEventListener('scroll', this.scrollHandler)
        this.scrollHandler()
      }, 200)
    }
  },
  created () {
    const highlight = this.$slots.highlight
    if (highlight && highlight[0]) {
      let code = ''
      let cur = highlight[0]
      if (cur.tag === 'pre' && cur.children && cur.children[0]) {
        cur = cur.children[0]
        if (cur.tag === 'code') {
          code = cur.children[0].text
        }
      }
      if (code) {
        this.codepen.html = stripTemplate(code)
        this.codepen.script = stripScript(code)
        this.codepen.style = stripStyle(code)
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      let highlight = this.$el.getElementsByClassName('highlight')[0]
      if (this.$el.getElementsByClassName('description').length === 0) {
        highlight.style.width = '100%'
        highlight.borderRight = 'none'
      }

      this._debouceResize = debounce(this.scrollHandler, 300)

      window.addEventListener('resize', this._debouceResize, false)

      // this.$once('hook:beforeDestroy', () => {})
    })
  },
  beforeDestroy () {
    this.removeScrollHandler()
    window.removeEventListener('resize', this._debouceResize, false)
  },
  methods: {
    /**
     * 复制code
     */
    onCopyCode () {
      if (this.copied) return
      const pre = this.$el.querySelectorAll('pre')[0]
      pre.setAttribute('contenteditable', 'true')
      pre.focus()
      document.execCommand('selectAll', false, null)
      this.copied = document.execCommand('copy')
      pre.removeAttribute('contenteditable')
      this.copied = false
      this.$notify({ type: 'success', message: '复制成功' })
    },

    /**
     * 滚动监听
     */
    scrollHandler () {
      // const { top, bottom, left, width } = this.$refs.meta.getBoundingClientRect()
      // this.fixedControl =
      //   bottom > document.documentElement.clientHeight && top + 44 <= document.documentElement.clientHeight
      // this.$refs.control.style.left = this.fixedControl ? `${left}px` : '0'
      // this.$refs.control.style.width = this.fixedControl ? `${width}px` : ''
    },

    /**
     * 移除监听事件
     */
    removeScrollHandler () {
      this.scrollParent && this.scrollParent.removeEventListener('scroll', this.scrollHandler)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .hljs {
  line-height: 1.75;
  font-family: Menlo, Monaco, Consolas, Courier, monospace;
  font-size: 12px;
  padding: 18px 24px;
  background-color: #fafafa;
  border: solid 1px #eaeefb;
  margin-bottom: 25px;
  border-radius: 4px;
  -webkit-font-smoothing: auto;
}
.demo-block {
  border: solid 1px #ebebeb;
  border-radius: 3px;
  transition: 0.2s;
  font-family: 'PingFangSC-Medium';
  & + .demo-block {
    margin-top: 12px;
  }
  &.hover {
    box-shadow: 0 0 8px 0 rgba(232, 237, 250, 0.6), 0 2px 4px 0 rgba(232, 237, 250, 0.5);
  }
  code {
    font-family: Menlo, Monaco, Consolas, Courier, monospace;
  }
  .demo-button {
    float: right;
  }
  .source {
    padding: 24px;
  }
  .meta {
    background-color: #fafafa;
    border-top: solid 1px #eaeefb;
    overflow: hidden;
    height: 0;
    transition: height 0.2s;
  }
  .description {
    padding: 20px;
    box-sizing: border-box;
    border: solid 1px #ebebeb;
    border-radius: 3px;
    font-size: 14px;
    line-height: 22px;
    color: #666;
    word-break: break-word;
    margin: 10px;
    background-color: #fff;
    p {
      margin: 0;
      line-height: 26px;
    }
    code {
      color: #5e6d82;
      background-color: #e6effb;
      margin: 0 4px;
      display: inline-block;
      padding: 1px 5px;
      font-size: 12px;
      border-radius: 3px;
      height: 18px;
      line-height: 18px;
    }
  }
  .highlight {
    //max-height: 50vh;
    //overflow-y: scroll;
    pre {
      margin: 0;
    }
    code.hljs {
      margin: 0;
      border: none;
      max-height: none;
      border-radius: 0;
      &::before {
        content: none;
      }
    }
  }
  .demo-block-control {
    border-top: solid 1px #eaeefb;
    height: 40px;
    box-sizing: border-box;
    background-color: #fff;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    text-align: center;
    margin-top: -1px;
    color: #d3dce6;
    cursor: pointer;
    position: relative;

    &.is-fixed {
      position: fixed;
      bottom: 0;
    }
    i {
      font-size: 16px;
      line-height: 40px;
      transition: 0.3s;
      &.hovering {
        transform: translateX(-40px);
      }
    }
    > span {
      position: absolute;
      transform: translateX(-30px);
      font-size: 14px;
      line-height: 40px;
      transition: 0.3s;
      display: inline-block;
    }
    &:hover {
      color: #409eff;
      background-color: #f9fafc;
    }
    & .text-slide-enter,
    & .text-slide-leave-active {
      opacity: 0;
      transform: translateX(10px);
    }

    .control-button {
      line-height: 40px;
      position: absolute;
      top: 0;
      right: 0;
      font-size: 14px;
      transform: translateX(-10px);
    }
  }
}
</style>

<style lang="scss">
.element-doc {
  font-family: 'PingFangSC-Medium';
  & > h2 {
    margin: 30px 0 20px;
    font-size: 16px;
    &:first-child {
      margin-top: 10px;
    }
  }
  & > table {
    $border: 1px solid #c9c9c9;
    word-break: break-all;
    margin: 12px 0;
    overflow-x: auto;
    display: table;
    width: 100%;
    table-layout: fixed;
    font-size: 13px;

    text-align: left;
    border-top: $border;
    border-left: $border;
    code {
      font-family: 'PingFangSC-Medium';
    }
    thead {
      tr {
        &:first-child {
          background: rgba($--color-primary, 0.14);
        }
      }
    }
    tbody {
      tr {
        td {
          &:first-child {
            background: rgba($--color-primary, 0.14);
          }
        }
      }
    }
    tr {
      border-top: none;
      &:nth-child(2n) {
        background-color: transparent;
      }
      th {
        color: #333;
        padding: 0.6em 1em;
        border: none;
        font-weight: 500;
        &:nth-child(1) {
          width: 22%;
        }
        &:nth-child(2) {
          width: 30%;
        }
        &:nth-child(3) {
          width: 130px;
        }
      }
      td,
      th {
        position: relative;
        margin: 0;
        color: #333;
        padding: 0.5em 1em;
        border-right: $border;
        border-bottom: $border;
        //&:after {
        //  background-color: $--table-border-color-base;
        //  content: '';
        //  position: absolute;
        //  display: block;
        //  left: 0;
        //  right: 0;
        //  bottom: 0;
        //  height: 1px;
        //  width: 100%;
        //  //transform: scaleY(0.5);
        //}
      }
      code {
        display: inline-block;
        margin: 2px 0 !important;
      }
    }
  }
}
</style>
